//
//  HJDLoginViewController.m
//  JYHospital
//
//  Created by 胡金东 on 16/6/1.
//  Copyright © 2016年 胡金东. All rights reserved.
//

#import "HJDLoginViewController.h"
#import "HJDResigesViewController.h"
#import "HJDForgetViewController.h"
#import "AFNetworking.h"
#import "MBProgressHUD.h"
#import "HJDRootTool.h"
#import "HJDNavigationController.h"
#import "HJDPesonalCenterViewController.h"
#import "HJDTabBarController.h"
#import "RESideMenu.h"

@interface HJDLoginViewController ()<UIScrollViewDelegate>

@property (nonatomic,strong)  UIView *backgroundView;
@property (nonatomic,strong) UITextField  *phoneField ;

@property (nonatomic,strong)  UITextField  *passwordtextField ;

@property (nonatomic,strong) NSArray * mapArray;

@property (nonatomic,strong)UIScrollView *scroll;




@end

@implementation HJDLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self setUpTextFile];
    [self setupBtn];
    //添加手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped:)];
    tap.cancelsTouchesInView = NO;
    [self.view addGestureRecognizer:tap];
    
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(popToPre) image:@"︿" HighImage:nil];
    
    
}
//返回
-(void)popToPre
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
//隐藏键盘
-(void)viewTapped:(UITapGestureRecognizer*)tap
{
    
    [self.view endEditing:YES];
    
}


-(void)setUpTextFile
{
    self.scroll = [[UIScrollView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.scroll.delegate = self;
    
    self.automaticallyAdjustsScrollViewInsets = YES;
    self.scroll.showsVerticalScrollIndicator = YES;//垂直移动
    self.scroll.showsHorizontalScrollIndicator = NO;//水平移动
    //上下滑动
    self.scroll.contentSize = CGSizeMake(HJDWidth,855 ) ;
    
    
    [self.view addSubview: self.scroll];
    
    self.backgroundView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, HJDWidth, HJDHeight- 50)];
    self.backgroundView.backgroundColor = [UIColor getColorWithHexNumber:@"F6F6F6"];
    [self.view addSubview:self.backgroundView];
    
    CGFloat phoneX = 46;
    CGFloat phoneY = HJDHeight - 350;
    CGFloat phoneW = HJDWidth - 46*2;
    CGFloat phoneH = 40;
    self.phoneField = [[UITextField alloc]initWithFrame:CGRectMake(phoneX, phoneY, phoneW, phoneH)];
    self.phoneField.placeholder = @"请输入本人手机号";
     [self.phoneField setValue:[UIFont systemFontOfSize:18] forKeyPath:@"_placeholderLabel.font"];
    [self.phoneField setValue:[UIColor getColorWithHexNumber:@"B3B3B3"] forKeyPath:@"_placeholderLabel.textColor"];
    self.phoneField.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 25,phoneH )];
    self.phoneField.leftViewMode = UITextFieldViewModeAlways;
    UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(0, 10, 21, 21)];
    image.image = [UIImage imageNamed:@"手机号"];
    self.phoneField.clearButtonMode = UITextFieldViewModeAlways;
    self.phoneField.keyboardType = UIKeyboardTypeNumberPad;
    [self.phoneField.leftView addSubview:image];
    
    [self.view addSubview:self.phoneField];
    
    //    横线
    CGFloat viewX = 46 ;
    CGFloat viewY = CGRectGetMaxY(self.phoneField.frame);
    CGFloat viewW = phoneW;
    CGFloat viewH = 1;
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(viewX, viewY, viewW, viewH)];
    view.backgroundColor = [UIColor getColorWithHexNumber:@"B3B3B3"];
    [self.view addSubview:view];
    
    CGFloat passwordX = 46  ;
    CGFloat passwordY = CGRectGetMaxY(view.frame) + HJDVerticalMargin;
    CGFloat passwordW = phoneW;
    CGFloat passwordH = phoneH;
    self.passwordtextField = [[UITextField alloc]initWithFrame:CGRectMake(passwordX, passwordY,passwordW, passwordH)];
    self.passwordtextField.placeholder = @"请输入密码";
    [self.passwordtextField setValue:[UIFont systemFontOfSize:18] forKeyPath:@"_placeholderLabel.font"];
    [self.passwordtextField setValue:[UIColor getColorWithHexNumber:@"B3B3B3"] forKeyPath:@"_placeholderLabel.textColor"];
    self.passwordtextField.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 25,phoneH )];
    self.passwordtextField.leftViewMode = UITextFieldViewModeAlways;
    UIImageView *passimage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 8, 16, 21)];
    passimage.image = [UIImage imageNamed:@"密码"];
    self.passwordtextField.clearButtonMode = UITextFieldViewModeAlways;
    [self.passwordtextField.leftView addSubview:passimage];
    
    [self.view addSubview:self.passwordtextField];
    //    横线
    CGFloat passwordviewX = 46 ;
    CGFloat passwordviewY = CGRectGetMaxY(self.passwordtextField.frame);
    CGFloat passwordviewW = phoneW;
    CGFloat passwordviewH = 1;
    UIView *passwordview = [[UIView alloc]initWithFrame:CGRectMake(passwordviewX, passwordviewY, passwordviewW, passwordviewH)];
   
    passwordview.backgroundColor = [UIColor getColorWithHexNumber:@"B3B3B3"];
    [self.view addSubview:passwordview];
    
    CGFloat loginbtnX = 46;
    CGFloat loginbtnY = CGRectGetMaxY(passwordview.frame) + 38;
    CGFloat loginbtnW = phoneW;
    CGFloat loginbtnH = 60;
    
    UIButton *loginbtn = [[UIButton alloc]initWithFrame:CGRectMake(loginbtnX, loginbtnY, loginbtnW, loginbtnH)];
    [loginbtn setTitle:@"登录" forState:UIControlStateNormal];
    loginbtn.titleLabel.font = [UIFont systemFontOfSize:25];
    [loginbtn setRadius:10 borderWith:0 borderColor:nil];
    [loginbtn setBackgroundColor:[UIColor getColorWithHexNumber:@"5CC6AC"]];
    [loginbtn addTarget:self action:@selector(loginbtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:loginbtn];

}

-(void)setupBtn
{
    CGFloat registerX = HJDWidth/4-50/2;
    CGFloat registerY = CGRectGetMaxY(self.backgroundView.frame);
    CGFloat registerW = 40;
    CGFloat registerH = 50;
    UIButton *registerBtn = [[UIButton alloc]initWithFrame:CGRectMake(registerX, registerY, registerW, registerH)];
    registerBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [registerBtn setTitle:@"注册" forState:UIControlStateNormal];
    [registerBtn setTitleColor:[UIColor getColorWithHexNumber:@"BCBCBC"] forState:UIControlStateNormal];
    [registerBtn addTarget:self action:@selector(registerBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:registerBtn];
//    竖线
    CGFloat viewX = HJDWidth/2 ;
    CGFloat viewY = CGRectGetMaxY(self.backgroundView.frame) + HJDVerticalMargin/2;
    CGFloat viewW = 1;
    CGFloat viewH = 30;
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(viewX, viewY, viewW, viewH)];
    view.backgroundColor = [UIColor getColorWithHexNumber:@"B3B3B3"];
    [self.view addSubview:view];
    
    CGFloat forgetX = CGRectGetMaxX(view.frame) + 48;
    CGFloat forgetY = CGRectGetMaxY(self.backgroundView.frame);
    CGFloat forgetW = 60;
    CGFloat forgetH = 50;
    UIButton *forgetBtn = [[UIButton alloc]initWithFrame:CGRectMake(forgetX, forgetY, forgetW, forgetH)];
    forgetBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [forgetBtn setTitle:@"忘记密码" forState:UIControlStateNormal];
    [forgetBtn setTitleColor:[UIColor getColorWithHexNumber:@"FFA0A0"] forState:UIControlStateNormal];
    [forgetBtn addTarget:self action:@selector(forgetBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:forgetBtn];
    
}

-(void)loginbtnClick
{
    NSString *url = @"/app/login.do";
    NSString *path = [Hurl stringByAppendingString:url];
    AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:self.phoneField.text forKey:@"phone"];
    [params setValue:self.passwordtextField.text forKey:@"password"];
   
    [mgr POST:path parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        

        

   
        
        NSDictionary *infoDic = (NSDictionary *)responseObject;
        NSString *heaeImage = infoDic[@"resultMap"][@"headurl"];
        NSUserDefaults *heaede = [NSUserDefaults standardUserDefaults];
        [heaede setObject:heaeImage forKey:@"headImage"];
        
        NSDictionary *infoDi = (NSDictionary *)responseObject;
        NSString *phone = infoDi[@"resultMap"][@"phone"];
        NSUserDefaults *phonede = [NSUserDefaults standardUserDefaults];
        [phonede setObject:phone forKey:@"phonenum"];
        
        NSDictionary *infoDictionary = (NSDictionary *)responseObject;
        NSString *info = infoDictionary[@"resultMap"];
        NSData *dataS = [NSKeyedArchiver archivedDataWithRootObject:info];
        [[NSUserDefaults standardUserDefaults] setValue:dataS forKey:@"userS"];
        
        NSDictionary *infoDicS = (NSDictionary *)responseObject;
       
        //        绑定登录要把name替换掉
        NSString *successfolStr = infoDicS[@"resultCode"];
        NSLog(@"success-------%@",successfolStr);
        
        //       保存
//        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
//        [defaults setObject:successfolStr forKey:@"myKey"];


//        NSDictionary *infoDic = (NSDictionary *)responseObject;
//        NSString *info = infoDic[@"resultMap"][@"name"];
//
//        NSData *data = [NSKeyedArchiver archivedDataWithRootObject:info];
//        [[NSUserDefaults standardUserDefaults] setValue:data forKey:@"userrr"];
        
        if ([successfolStr isEqualToString:@"001" ]) {
            
                HJDTabBarController *tabBar = [[HJDTabBarController alloc]init];
                        HJDPesonalCenterViewController *personalVC = [[HJDPesonalCenterViewController alloc] init];
            
                        RESideMenu *sideMenuViewController = [[RESideMenu alloc]initWithContentViewController: tabBar leftMenuViewController:personalVC  rightMenuViewController:nil];
            
                        sideMenuViewController.backgroundImage = [UIImage imageNamed:@"bg"];
                        sideMenuViewController.menuPreferredStatusBarStyle = 1; // UIStatusBarStyleLightContent
                        sideMenuViewController.delegate = self;
                        sideMenuViewController.contentViewShadowColor = [UIColor blackColor];
                        sideMenuViewController.contentViewShadowOffset = CGSizeMake(0, 0);
                        sideMenuViewController.contentViewShadowOpacity = 0.2;
                        sideMenuViewController.contentViewShadowRadius = 12;
                        sideMenuViewController.contentViewShadowEnabled = YES;
           
            // 切换根控制器:可以直接把之前的根控制器清空
            
                HJDKeyWindow.rootViewController = sideMenuViewController;

//           [HJDRootTool chooseNewFeatureRootViewController:HJDKeyWindow];
            [MBProgressHUD showSuccess:@"登录成功"];
            
        }
        else if ( [successfolStr isEqualToString:@"002"])
        {
            [MBProgressHUD showError:@"密码或着账号不对"];
        }
        
     
        NSLog(@"yes'''''''''''%@",responseObject);

        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [MBProgressHUD showError:@"登录失败"];
        NSLog(@"%@",error);
    }];
   
   }

-(void)registerBtnClick
{
    HJDResigesViewController * registerVC = [[HJDResigesViewController alloc]init];
    HJDNavigationController *nav = [[HJDNavigationController alloc]initWithRootViewController:registerVC];

    [self presentViewController:nav animated:YES completion:nil];
    
    
    NSData *data3 = [[NSUserDefaults standardUserDefaults ]valueForKey:@"userS"];
    NSString *info3 = [NSKeyedUnarchiver unarchiveObjectWithData:data3];
    
    NSLog(@"userS---------%@-----",info3);
    
//    NSData *data2 = [[NSUserDefaults standardUserDefaults ]valueForKey:@"userrr"];
//    NSString *info2 = [NSKeyedUnarchiver unarchiveObjectWithData:data2];
//   
//    NSLog(@"userrr---------%@-----",info2);
//    
//    ;
   
    
     }

-(void)forgetBtnClick
{
    HJDForgetViewController * forgetVC = [[HJDForgetViewController alloc]init];
    HJDNavigationController *nav = [[HJDNavigationController alloc]initWithRootViewController:forgetVC];
    
    [self presentViewController:nav animated:YES completion:nil];
}

@end
